<template>
  <a-layout
    id="components-layout-demo-side"
    style="min-height: 100vh;min-width:750px"
  >
    <a-layout-sider collapsible>
      <div class="logo">
        <img src="../assets/logo.png" alt="" />
      </div>
      <!-- 一级菜单 -->
      <a-menu theme="dark" :default-selected-keys="['1']" mode="inline">
        <a-sub-menu v-for="item in menuList" :key="item.id">
          <span slot="title">
            <a-icon :type="icon[item.id]" style="fontSize:18px;" />
            <span>{{ item.authName }}</span>
          </span>
          <!-- 二级菜单 -->
          <a-menu-item
            v-for="subItem in item.children"
            :key="subItem.id"
            @click="
              saveNavState('/' + subItem.path, item.authName, subItem.authName)
            "
          >
            <a-icon type="appstore" />
            <span>{{ subItem.authName }}</span>
          </a-menu-item>
        </a-sub-menu>
      </a-menu>
    </a-layout-sider>
    <a-layout style="min-width:750px">
      <a-layout-header class="header">
        电商后台管理系统
        <a-button type="danger" @click="logout()">退出</a-button>
      </a-layout-header>

      <a-layout-content style="margin: 0 24px">
        <!-- 面包屑 -->
        <Breadcrumb></Breadcrumb>

        <!-- 卡片视图区域 -->
        <div
          :style="{
            padding: '24px',
            background: '#fff',
            minHeight: '360px'
          }"
        >
          <router-view></router-view>
        </div>
      </a-layout-content>
      <a-layout-footer style="text-align:center">
        电商后台管理系统 Created by Ceang
      </a-layout-footer>
    </a-layout>
  </a-layout>
</template>

<script>
import Breadcrumb from '../components/Breadcrumb'
export default {
  components: { Breadcrumb },
  created() {
    this.getMenuList()
    this.activePath = window.sessionStorage.getItem('activePath')
  },
  data() {
    return {
      menuList: [],
      activePath: '',
      icon: {
        125: 'team',
        103: 'key',
        101: 'shopping',
        102: 'ordered-list',
        145: 'line-chart'
      }
    }
  },

  methods: {
    logout() {
      window.sessionStorage.clear()
      this.$router.push('/login')
    },
    // 获取左侧菜单数据
    async getMenuList() {
      const { data: res } = await this.$http.get('menus')
      if (res.meta.status !== 200) {
        return this.$message.error('错误代码:' + res.data.status)
      }
      this.menuList = res.data
      console.log(this.menuList)
    },
    saveNavState(activePath, itemName, subItemName) {
      window.sessionStorage.setItem('activePath', activePath)
      this.activePath = activePath
      this.$router.push(activePath)
    }
  }
}
</script>

<style lang="less" scoped>
#components-layout-demo-side {
  .logo {
    display: flex;
    justify-content: center;
    align-items: center;
    height: 32px;
    background: rgba(255, 255, 255, 0.2);
    margin: 16px;
    img {
      width: auto;
      height: 100%;
    }
  }
  .header {
    display: flex;
    justify-content: space-between;
    background: #fff;
    padding: 10px;
    align-items: center;
    font-size: 24px;
  }
}
</style>
